package com.lb.shopguide.adapter;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.market.ActivityApplyBean;
import com.lb.shopguide.entity.market.MyActivityBean;
import com.lb.shopguide.util.pic.ImageLoaderUtils;

import java.util.List;

public class AdapterMyActivity extends BaseQuickAdapter<MyActivityBean, BaseViewHolder> {
    public AdapterMyActivity(int layoutResId, @Nullable List<MyActivityBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final MyActivityBean item) {
        View view = helper.itemView;
        ImageView ivAct = view.findViewById(R.id.iv_my_activity);
        TextView tvActName = view.findViewById(R.id.tv_act_name);
        TextView tvActTime = view.findViewById(R.id.tv_act_time);
        TextView tvActShop = view.findViewById(R.id.tv_act_shop);
        TextView tvActState = view.findViewById(R.id.tv_act_state);
        ImageView ivNewTag = view.findViewById(R.id.iv_new_tag);
        LinearLayout lyaoutDetail = view.findViewById(R.id.layout_detail);

        ImageLoaderUtils.display(mContext, ivAct, item.getActivityPic());
        tvActName.setText(item.getActivityName());
        tvActTime.setText(item.getActBeginTime() + "至" + item.getActEndTime());
        tvActShop.setText(item.getStoreName());
        if (item.isNewActivityFlag()) {
            ivNewTag.setVisibility(View.VISIBLE);
        } else {
            ivNewTag.setVisibility(View.GONE);
        }
        //活动状态(1：进行中 2：等待开启 3：已过期 4：已关闭)
        Drawable drawableRun = mContext.getResources().getDrawable(R.drawable.bg_round_corner_activity_run);
        Drawable drawableClose = mContext.getResources().getDrawable(R.drawable.bg_round_corner_activity_close);
        switch (item.getActivityStatus()) {
            case AppConstant.ACTIVITY_RUNNING:
                tvActState.setText("进行中");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tvActState.setBackground(drawableRun);
                } else {
                    tvActState.setBackgroundDrawable(drawableRun);
                }
                break;
            case AppConstant.ACTIVITY_WAIT_OPEN:
                tvActState.setText("等待开启");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tvActState.setBackground(drawableRun);
                } else {
                    tvActState.setBackgroundDrawable(drawableRun);
                }
                break;
            case AppConstant.ACTIVITY_OUT_OF_DATE:
                tvActState.setText("已到期");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tvActState.setBackground(drawableClose);
                } else {
                    tvActState.setBackgroundDrawable(drawableClose);
                }
                break;
            case AppConstant.ACTIVITY_CLOSE:
                tvActState.setText("已关闭");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    tvActState.setBackground(drawableClose);
                } else {
                    tvActState.setBackgroundDrawable(drawableClose);
                }
                break;
        }
        tvActState.setVisibility(View.VISIBLE);
        lyaoutDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDetailClickListener != null) {
                    mOnDetailClickListener.onDetailClick(item.getApplyCode(),item.getActivityDescPic());
                }
            }
        });
    }

    public interface OnDetailClickListener {
        void onDetailClick(String applyCode, String picUrl);
    }

    private OnDetailClickListener mOnDetailClickListener;

    public void setOnDetailClickListener(OnDetailClickListener listener) {
        mOnDetailClickListener = listener;
    }
}
